package com.wuad.platformwuad.module.Excel.controller;

import com.google.protobuf.ServiceException;
import com.wuad.platformwuad.util.ExcelPoiUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 下载自定义Excel模板
 *
 * @author wuad
 * @date 2023年08月09日 17:51
 */
@RequestMapping("/excelCaozuo")
@RestController
public class ExcelCaozuoController {

    @RequestMapping("/downloadPlatform")
    public void downloadPlatform(HttpServletResponse response) {

        //表格标题，就是模型的属性名
        String[] titles = {"给你补上一个","单行文本不超过50字", "电话号码不超过12字", "微信号6-20字", "单选多选 文字提示按要求输入",
                "数字10位以内数字","日期格式年月日","日期时间格式：年月日时分秒","省市区提示","跟进人 提示输入姓名"};

        // 表格sheet名
        String sheetName = "员工信息表";

        //调用common下的Excel导出方法，导出Excel
        HSSFWorkbook workbook = ExcelPoiUtils.downloadExcelByPOIUtils(sheetName, titles);

        try {
            //文件名，需要编码成ISO8859-1
            String fileName = new String("员工信息表.xls".getBytes("UTF-8"), "ISO8859-1");
            //设置响应头，返回前端下载文件
            response.setHeader("content-Disposition", "attachment;filename=" + fileName);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取excel
     *
     */
    @PostMapping("/readExcel")
    public void readExcel(@RequestParam("file") MultipartFile file) throws ServiceException {
        ExcelPoiUtils.readExcel(file);
    }
}
